package com.livelab.java;

import com.livelab.model.ExerciseSummary;
import com.livelab.util.MySQLDatabaseUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 * Backing bean that supports "exerciseSummary.xhtml"
 * 
 * @author Dakota
 */
@ManagedBean
@RequestScoped
public class ExerciseSummaryBean {
    MySQLDatabaseUtil db;       //database manager
    ResultSet temp;
       
    double maxTotalScore = 0;   //summation of max scores from all assigned exercises
    double totalScore = 0;      //summation of accumulated scores
    double percentage;          //percentage of totalScore/maxTotalscore
    
    int numAssigned;            //number of assigned exercises
    
    
    List<ExerciseSummary> exerciseSummary;  //list of individual exercise summarys
    
    String instructorUsername;  //student's instructor's username
    
    @ManagedProperty(value = "#{studentLoginBean}")
    private StudentLoginBean student; //student login bean reference
   
    /**
     * Constructor
     */
    public ExerciseSummaryBean() {
         db = new MySQLDatabaseUtil("localhost/javalivelab9e", "agsuser", "agsuser");
         exerciseSummary = new ArrayList<ExerciseSummary>();
    }
    
    @PostConstruct
    public void init(){
        ResultSet rs;
         rs = db.executeQuery("SELECT agsinstructor.username AS username FROM agsinstructor, agsstudent WHERE agsstudent.username = '" + student.getUsername() + "' AND agsinstructor.courseID = '" + student.getCourseId() + "';");
         try {
            if (rs.next()) {
                instructorUsername = rs.getString("username");
                
                //select all exercises assigned by the instructor
                temp = db.executeQuery("SELECT e.exerciseName as exerciseName, e.dueDate as dueDate, e.score as maxScore FROM exerciseassigned e WHERE e.username = '" + instructorUsername + "';");
                while(temp.next()){
                    ResultSet singleScore = db.executeQuery("SELECT score from agslog where username = '" + student.getUsername() + "' AND exerciseName = '" + temp.getString(1) + "';");
                    //if attempted by student
                    if(singleScore.next()){
                        exerciseSummary.add(new ExerciseSummary(temp.getString(1), temp.getTimestamp(2), singleScore.getDouble(1), temp.getDouble(3)));
                        //accumulate student's score
                        totalScore += singleScore.getDouble(1);
                    //if not attempted by student, give score of 0
                    }else{
                        exerciseSummary.add(new ExerciseSummary(temp.getString(1), temp.getTimestamp(2), 0, temp.getDouble(3)));
                    }
                    singleScore.close();
                    //accumulate max score
                    maxTotalScore += temp.getDouble(3);
                }
                
                //get percentage
                percentage = (totalScore/maxTotalScore) * 100;
                temp.close();
            }
         } catch(SQLException ex){
             ex.printStackTrace();
         }
         //record the number of exercises
         numAssigned = exerciseSummary.size();
    }
    
    /*
     * GETTERS
     */
    public double getMaxTotalScore(){
        return maxTotalScore;
    }
    
    public double getPercentage(){
        return percentage;
    }
    
    public double getTotalScore(){
        return totalScore;
    }
    
    public int getNumAssigned(){
        return numAssigned;
    }
    
    public List<ExerciseSummary> getExerciseSummary(){
        return exerciseSummary;
    }
      
    /*
     * MANAGED PROPERTY SETTER
     */
    public void setStudent(StudentLoginBean student) {
        this.student = student;
    }
}
